import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my_flutter_github/common/dao/ReposDao.dart';
import 'package:my_flutter_github/model/RepoCommit.dart';
import 'package:my_flutter_github/model/Repository.dart';
import 'package:my_flutter_github/page/RepositoryDetailPage.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/utils/UserEventUtils.dart';
import 'package:my_flutter_github/widgets/BaseListState.dart';
import 'package:my_flutter_github/widgets/CommonOptionWidget.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:my_flutter_github/widgets/ReposHeaderItemWidget.dart';
import 'package:my_flutter_github/widgets/UserEventItemWidget.dart';

/// 仓库详情动态信息页面
class ReposDetailInfoPage extends StatefulWidget {
  final String userName;

  final String reposName;

  final ReposDetailParentControl reposDetailParentControl;

  final OptionControl titleOptionControl;

  ReposDetailInfoPage(this.userName, this.reposName, this.reposDetailParentControl, this.titleOptionControl, {Key key}) : super(key: key);

  @override
  ReposDetailInfoPageState createState() => ReposDetailInfoPageState(userName, reposName, reposDetailParentControl, titleOptionControl);
}

// ignore: mixin_inherits_from_not_object
class ReposDetailInfoPageState extends BaseListState<ReposDetailInfoPage> {
  final String userName;

  final String reposName;

  final ReposDetailParentControl reposDetailParentControl;

  final OptionControl titleOptionControl;

  Repository repository = Repository.empty();

  int selectIndex = 0;

  ReposDetailInfoPageState(this.userName, this.reposName, this.reposDetailParentControl, this.titleOptionControl);

  ///渲染时间Item或者提交Item
  _renderEventItem(index) {
    if (index == 0) {
      return new ReposHeaderItemWidget(ReposHeaderViewModel.fromHttpMap(userName, reposName, repository), (index) {
        selectIndex = index;
        clearData();
        showRefreshLoading();
      });
    }

    if (selectIndex == 1) {
      ///提交
      return new UserEventItemWidget(
        EventViewModel.fromCommitMap(pullLoadWidgetControl.dataList[index - 1]),
        onPressed: () {
          RepoCommit model = pullLoadWidgetControl.dataList[index - 1];
          NavigatorUtils.goPushDetailPage(context, userName, reposName, model.sha, false);
        },
        needImage: false,
      );
    }
    return new UserEventItemWidget(
      EventViewModel.fromEventMap(pullLoadWidgetControl.dataList[index - 1]),
      onPressed: () {
        UserEventUtils.ActionUtils(context, pullLoadWidgetControl.dataList[index - 1], userName + "/" + reposName);
      },
    );
  }

  ///获取列表
  _getDataLogic() async {
    if (selectIndex == 1) {
      return await ReposDao.getReposCommitsDao(
        userName,
        reposName,
        page: pageIndex,
        branch: reposDetailParentControl.currentBranch,
        needDb: pageIndex <= 1,
      );
    }
    return await ReposDao.getRepositoryEventDao(
      userName,
      reposName,
      page: pageIndex,
      branch: reposDetailParentControl.currentBranch,
      needDb: pageIndex <= 1,
    );
  }

  ///获取详情
  _getReposDetail() {
    ReposDao.getRepositoryDetailDao(userName, reposName, reposDetailParentControl.currentBranch).then((result) {
      if (result != null && result.result) {
        setState(() {
          repository = result.data;
          titleOptionControl.url = repository.htmlUrl;
        });
        return result.next;
      }
      return new Future.value(null);
    }).then((result) {
      if (result != null && result.result) {
        setState(() {
          repository = result.data;
          titleOptionControl.url = repository.htmlUrl;
        });
      }
    });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  requestRefresh() async {
    _getReposDetail();
    return await _getDataLogic();
  }

  @override
  requestLoadMore() async {
    return await _getDataLogic();
  }

  @override
  bool get autoRefresh => true;

  @override
  bool get needHeader => true;

  @override
  Widget build(BuildContext context) {
    super.build(context); // See AutomaticKeepAliveClientMixin.
    return PullLoadWidget(
      control: pullLoadWidgetControl,
      itemBuilder: (BuildContext context, int index) => _renderEventItem(index),
      refreshCallback: onRefresh,
      loadMoreCallback: onLoadMore,
      refreshKey: refreshIndicatorKey,
    );
  }
}
